// package-market/pages/lottery-preview/index.js
import { showLoading, hideLoading, redirectTo } from '../../../common/env';
import { PACKAGE_MARKET_PAGE } from '../../../constants/route';
import { ManageTargetInfoUpdateMixins } from '../../../behaviors/page';
import { getActivityDetail, publishActivity } from '../../../api/brd';
let loaded = false, brandId, activityId;

Page({
    mixins: [ManageTargetInfoUpdateMixins],
    data: {
        ownerInfo: {},
        activityInfo: {},
    },
    onLoad(options) {
        brandId = options.brandId;
        activityId = options.activityId;
    },
    onShow() {
        this._handleGetDetail();
    },
    async _handleGetDetail() {
        !loaded && showLoading('加载中...');
        const ownerInfo = this.data.manageTargetInfo;
        const activityInfo = await getActivityDetail({ brandId, activityId });
        this.setData({ ownerInfo, activityInfo });
        if (!loaded) hideLoading(); loaded = true;
    },
    async handlePublish() {
        const { activityInfo } = this.data;
        showLoading('发布中...');
        const flag = await publishActivity({
            brandId: brandId || activityInfo.brandId,
            activityId: activityId || activityInfo.activityId,
        });
        if (flag) {
            hideLoading();
            redirectTo(PACKAGE_MARKET_PAGE['lotteryDetail'], {
                activityId: activityId || activityInfo.activityId,
                showSuccess: true,
            });
        }
    },
    handleNavBack() {
        const { activityInfo } = this.data;
        redirectTo(PACKAGE_MARKET_PAGE['lotteryEdit'], {
            brandId: brandId || activityInfo.brandId,
            activityId: activityId || activityInfo.activityId,
        });
    },
})
